<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/25 0:20
 */

namespace app\admin\business;


use app\common\business\BusBase;
use app\common\lib\ArrayLib;
use app\common\model\mysql\AdminAuth as AdminAuthModel;
use think\facade\Log;

class AdminAuth extends BusBase {

    public $model = null;

    public function __construct() {
        $this->model = new AdminAuthModel();
    }

    /**
     * 获取权限列表数据
     * @param $data
     * @param int $limit
     * @return array
     */
    public function getLists($data, $limit = 10) {

        $likeKeys = []; // 如果没有定义这个 有时候会报错
        if (!empty($data)) {
            $likeKeys = array_keys($data);
        }
        try {
            $list = $this->model->getLists($likeKeys, $data, $limit);
            $result = $list->toArray();
//            $result['data'] = ArrayLib::getTree($result['data']);
        } catch (\Exception $e) {
            Log::error('common-business-AdminAuth-getLists: ' . $e->getMessage());
            $result = ArrayLib::getPaginateDefaultData($limit);
        }
        return $result;
    }

    /**
     * 权限列表获取数据
     * @param $data
     * @return array|bool
     */
    public function getAuthLists($data = []) {
        try {
            $list = $this->model->getNormalListsAll($data);
            $result = $list->toArray();
        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-getRoleLists: ' . $e->getMessage());
            return false;
        }
        return $result;
    }

    /**
     * 菜单数据获取
     * @param array $data
     * @param $rules
     * @return array|bool
     */
    public function getAuthMenuLists($data = [], $rules) {
        try {
            $list = $this->model->getNormalMenuListsAll($data, $rules);
            $result = $list->toArray();

            $menu = [];
            foreach ($result as $k => $v) {
                $menu[$k]['id'] = $v['id'];
                $menu[$k]['pid'] = $v['pid'];
                $menu[$k]['title'] = $v['title'];
                $menu[$k]['icon'] = 'fa ' . $v['icon'];
                $menu[$k]['href'] = $v['path'];
                $menu[$k]['target'] = '_self';
            }

        } catch (\Exception $e) {
            Log::error('common-business-AdminRole-getRoleLists: ' . $e->getMessage());
            return false;
        }
        return $menu;
    }

    /**
     * 新增权限
     * @param $data
     * @return bool|mixed
     */
    public function insertData($data) {
        // 数据处理
        $data['pid'] = empty($data['pid']) ? 0 : $data['pid'];
        $data['type'] = isset($data['type']) == "on" ? "1" : "0";
        if ($data['c'] && $data['a']) {
            $data['path'] = '/admin/' . $data['c'] . '/' . $data['a'];
        } else {
            $data['path'] = '';
        }

        try {
            $id = $this->add($data);
            if (!$id) {
                return $id;
            }
            return true;
        } catch (\Exception $e) {
            Log::error('common-business-AdminAuth-insertData: ' . $e->getMessage());
            return false;
        }
    }

    /**
     * 更新权限
     * @param $data
     * @return bool|mixed
     */
    public function updateData($data) {
        // 数据处理
        $data['pid'] = empty($data['pid']) ? 0 : $data['pid'];
        $data['type'] = isset($data['type']) == "on" ? "1" : "0";
        if ($data['c'] && $data['a']) {
            $data['path'] = '/admin/' . $data['c'] . '/' . $data['a'];
        } else {
            $data['path'] = '';
        }

        try {
            $id = $this->edit($data['id'],$data);
            if (!$id) {
                return $id;
            }
            return true;
        } catch (\Exception $e) {
            Log::error('common-business-AdminAuth-updateData: ' . $e->getMessage());
            return false;
        }
    }

}